// NetReceiver.h: interface for the CNetReceiver class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_NETRECEIVER_H__7791C211_A9E3_449D_904A_F7499D45D05B__INCLUDED_)
#define AFX_NETRECEIVER_H__7791C211_A9E3_449D_904A_F7499D45D05B__INCLUDED_

#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include "../../source/OS/OSThread.h"
#include "../../source/OS/OSMutex.h"

typedef struct tagNetDataInfo
{
	UInt32 dwFromIP;
	UInt16 wFromPort;
	UInt8  bChanID;
	UInt16 wReqID;
	UInt16 wDataLen;
	Int32  dwHandle;	//general it is socket fd
	UInt8* pData;
	tagNetDataInfo()
	{
		memset( this, 0, sizeof(*this) );
	}
}NetDataInfo,*PNetDataInfo;
typedef std::queue<NetDataInfo> Q_RECV_COMMPACK;

typedef std::queue<UInt8*> Q_IDLE_BUFFER;
#define IDLE_BUFFER_Q_MAX_LEN 16

#define RECV_BUFFER_MAX_SIZE 10240
#define RECEIVE_QUEUE_MAX_LEN	102400

class CNetReceiver : public OSTask  
{
public:
	static CNetReceiver& Instance();
	static void DelInstance();
	virtual ~CNetReceiver();

	Bool GetNetpack(PNetDataInfo pNetDataInfo);
	void ReclaimPack(PNetDataInfo pNetDataInfo);

private:
	CNetReceiver();
	virtual Bool Run();
	void PushCommPack(PNetDataInfo pNetDataInfo);
	
	void Release();
	
	UInt8* AllocIdleBuffer( UInt16 wBufLen );
	OSMutex m_MutexIdleBuffer;
	Q_IDLE_BUFFER m_qIdleBuffer;

	OSMutex m_MutexNetpack;
	Q_RECV_COMMPACK m_qNetpack;

private:
	static CNetReceiver * m_Instance;
};

#endif // !defined(AFX_NETRECEIVER_H__7791C211_A9E3_449D_904A_F7499D45D05B__INCLUDED_)
